﻿using Abp.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;

namespace SOEI.Solution.EntityFrameworkCore.EntityMapper
{
    public class AbpBaseMapper
    {

        #region ================================================ 表 索引 试图 主键 等名称相关 ======================================= 
        /// <summary>
        /// 获取关系名称转大写（超过30个自动截取）
        /// </summary>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <returns></returns>
        public string GetConstraintName(string firstName, string lastName)
        {
            var ConstraintName = string.Format("{0}{1}{2}", firstName, AppCoreConst.SchemaNames.TABLE_PREFIX, lastName);
            if (ConstraintName.Length > 30) {
                return ConstraintName.ToUpper().Substring(0, 30);
            }
            return ConstraintName.ToUpper();
        }

        /// <summary>
        /// 获取索引的名称转大写（超过30个自动截取）
        /// </summary>
        /// <param name="lastName">名称</param>
        /// <returns></returns>
        public string GetIndexName(string lastName)
        {
            var ConstraintName = string.Format("IX_{0}{1}", AppCoreConst.SchemaNames.TABLE_PREFIX, lastName);
            if (ConstraintName.Length > 30)
            {
                return ConstraintName.ToUpper().Substring(0, 30);
            }
            return ConstraintName.ToUpper();
        }

        /// <summary>
        /// 获取主键名称转大写(自动添加 PK_)
        /// </summary>
        /// <param name="lastName"></param>
        /// <returns></returns>
        public string GetPrimaryKeyName(string lastName)
        {
            return this.GetConstraintName("PK_", lastName);
        }

        /// <summary>
        /// 获取外键名称转大写(自动添加 FK_)
        /// </summary>
        /// <param name="lastName"></param>
        /// <returns></returns>
        public string GetForeignKeyName(string lastName)
        {
            return this.GetConstraintName("FK_", lastName);
        }

        /// <summary> 
        /// 获取数据表名称,转大写(自动添加 TABLE_PREFIX)【关联老系统库请勿使用】 
        /// </summary>
        /// <param name="lastName"></param>
        /// <returns></returns>
        public string GetTableName(string lastName)
        {
            var ConstraintName = string.Format("{0}{1}",AppCoreConst.SchemaNames.TABLE_PREFIX, lastName);
            if (ConstraintName.Length > 30)
            {
                return ConstraintName.ToUpper().Substring(0, 30);
            }
            return ConstraintName.ToUpper();
        }

        #endregion

    }
}
